package com.xinghou.timer.controller;

import com.xinghou.platform.entity.req.ShareInfoReq;
import com.xinghou.platform.model.main.UserRelationSchedule;
import com.xinghou.platform.server.api.bussiness.ShareInfoBusinessService;
import com.xinghou.platform.service.main.UserRelationScheduleService;
import com.xinghou.timer.common.AbstractConsumer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * Created by liufangyu on 2016/9/6.
 */
@Controller
public class RelationController extends AbstractConsumer {
    private static final Logger logger = LoggerFactory
            .getLogger(RelationController.class);
    private UserRelationScheduleService userRelationScheduleService =  this.getBean("UserRelationScheduleService");
    private ShareInfoBusinessService shareInfoBusinessService =  this.getBean("shareInfoService");

    @SuppressWarnings("deprecation")
    @Scheduled(cron = "0 */1 * * * ?")
    public void test(){
        System.out.println("timer task is start");
        Long nowtime = System.currentTimeMillis();
        logger.info("timer task is start");
        List<UserRelationSchedule> list =  userRelationScheduleService.getNeedList(nowtime);
        System.out.println("list size is "+ list.size());
        logger.info("list size is {}",list.size());
        for(UserRelationSchedule userRelationSchedule :list) {
            ShareInfoReq  sr = new ShareInfoReq();
            sr.setUserid(String.valueOf((int)(Math.random()*6000000)));
            sr.setToUserid(String.valueOf(userRelationSchedule.getUserid()));
            sr.setSharetype(String.valueOf(4));
            sr.setType(String.valueOf(1));
            try{
                shareInfoBusinessService.saveRelation(sr);
            }catch (Exception e){

            }
            System.out.println("遍历userid 为 "+userRelationSchedule.getUserid()+"|"+userRelationSchedule.getNext_time());
            int nowCount = userRelationSchedule.getNow_count();
            if(0<=nowCount && nowCount <10) {
               userRelationSchedule.setNext_time(nowtime+ 60*60*1000+(int)(Math.random()*2*60*60*1000));
            }else if(10<=nowCount && nowCount <20) {
                userRelationSchedule.setNext_time(nowtime+ 60*60*1000 *3+(int)(Math.random()*2*60*60*1000));
            }else if(20<=nowCount && nowCount <50) {
                userRelationSchedule.setNext_time(nowtime+ 60*60*1000 *8+(int)(Math.random()*2*60*60*1000));
            }else if(50<=nowCount && nowCount <=70) {
                userRelationSchedule.setNext_time(nowtime+ 60*60*1000 *10+(int)(Math.random()*2*60*60*1000));
            }
            System.out.println("遍历userid 为 "+userRelationSchedule.getUserid()+"|"+userRelationSchedule.getNext_time());
            userRelationScheduleService.addNowCount(userRelationSchedule);
        }
    }

    @Override
    protected void doWork(String task) {

    }
/*    public <T> T getBean(String name) {
        @SuppressWarnings("unchecked")
        T t = (T) ApplicationContext.getContext().getBean(name);
        return t;
    }*/
}
